package leetcode.offer;

import java.util.Stack;

/**
 * ClassName: Offer31
 * Description:leetcode - 剑指offer 31 栈的压入、弹出序列
 * date: 2021-11-22 21:57
 *
 * @author liyifan
 */
public class Offer31 {
    public boolean validateStackSequences(int[] pushed, int[] popped) {
        Stack<Integer> stack = new Stack<>();
        int i = 0;
        for (Integer x:pushed) {
            stack.push(x);
            while (!stack.isEmpty() && stack.peek() == popped[i]) {
                stack.pop();
                i++;
            }
        }
        return stack.empty();
    }
}
